import javax.swing.*;
import java.awt.*;

class Frame extends JFrame {
    Panel panel = new Panel();

    Frame() {
        setLayout(new BorderLayout());
        add(panel.displayPanel(), BorderLayout.NORTH);
        add(panel.keyPanel(), BorderLayout.CENTER);
        setBounds(200, 300, 350, 300);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setAlwaysOnTop(true);
    }
}

@SuppressWarnings("all")
class Panel {
    StringBuffer sb = new StringBuffer();
    JTextField displayT = new JTextField(33);
    Double operand1 = null;
    String operator = "";

    // 更新显示文本的方法
    private void updateDisplay() {
        displayT.setText(sb.toString());
    }

    JPanel displayPanel() {
        JPanel display = new JPanel();
        display.add(displayT);
        return display;
    }

    void dis(double x) {
        sb.setLength(0);
        sb.append(x);
        updateDisplay();
    }

    JPanel keyPanel() {
        JPanel key = new JPanel();
        key.setLayout(new GridLayout(4, 5, 5, 5));

        String[] buttonLabels = {"1", "2", "3", "+", "/",
                "4", "5", "6", "*", "-",
                "7", "8", "9", "c", "退格",
                "0", ".", "=", "+/-", "x^"};
        for (String label : buttonLabels) {
            JButton button = new JButton(label);
            button.setPreferredSize(new Dimension(65, 39));

            switch (label) {
                case "1":
                case "2":
                case "3":
                case "4":
                case "5":
                case "6":
                case "7":
                case "8":
                case "9":
                case "0":
                case ".":
                    button.addActionListener(e -> {
                        sb.append(label);
                        updateDisplay();
                    });
                    break;
                case "+":
                case "-":
                case "*":
                case "/":
                case "+/-":
                    button.addActionListener(e -> {
                        if (operand1 == null) {
                            try {
                                operand1 = Double.parseDouble(sb.toString());
                            } catch (NumberFormatException ex) {
                                operand1 = null;
                                JOptionPane.showMessageDialog(null, "Invalid input for operand1!", "Error", JOptionPane.ERROR_MESSAGE);
                            }
                            sb.setLength(0);
                            operator = label;
                            updateDisplay();
                        }
                    });
                    break;
                case "c":
                    button.addActionListener(e -> {
                        sb.setLength(0);
                        operand1 = null;
                        operator = "";
                        updateDisplay();
                    });
                    break;
                case "退格":
                    button.addActionListener(e -> {
                        if (sb.length() > 0) {
                            sb.setLength(sb.length() - 1);
                            if (operand1 == null && sb.length() == 0) {
                                operand1 = null;
                            }
                        }
                        updateDisplay();
                    });
                    break;
                case "=":
                    button.addActionListener(e -> {
                        Double operand2 = null;
                        try {
                            operand2 = Double.parseDouble(sb.toString());
                        } catch (NumberFormatException ex) {
                            operand2 = null;
                            JOptionPane.showMessageDialog(null, "Invalid input for operand2!", "Error", JOptionPane.ERROR_MESSAGE);
                            return;
                        }

                        double result = 0;
                        switch (operator) {
                            case "+":
                                result = operand1 + operand2;
                                break;
                            case "-":
                                result = operand1 - operand2;
                                break;
                            case "*":
                                result = operand1 * operand2;
                                break;
                            case "/":
                                if (operand2 != 0) {
                                    result = operand1 / operand2;
                                } else {
                                    JOptionPane.showMessageDialog(null, "除数为0", "Error", JOptionPane.ERROR_MESSAGE);
                                    return;
                                }
                                break;
                            case "+/-":
                                if (operand1 != null) {
                                    result = -operand1;
                                    operand1 = result;
                                } else {
                                    JOptionPane.showMessageDialog(null, "操作数为空", "Error", JOptionPane.ERROR_MESSAGE);
                                }
                                break;
                            default:
                                JOptionPane.showMessageDialog(null, "数据异常", "Error", JOptionPane.ERROR_MESSAGE);
                                return;
                        }

                        dis(result);
                        operand1 = result;
                    });
                    break;
                case "x^":
                    button.addActionListener(e -> {
                        JOptionPane.showMessageDialog(null, "尚未实现该功能", "Info", JOptionPane.INFORMATION_MESSAGE);
                    });
                    break;
            }

            key.add(button);
        }
        return key;
    }
}

public class work01 {
    public static void main(String[] args) {
        new Frame();
    }
}